//
//  SCEatPlayStoreDetailController.m
//  shangcheng
//
//  Created by 张艳江 on 2019/7/1.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCEatPlayStoreDetailController.h"
#import "SCDetailSetion_EPView.h"
#import "SCRecommendGoods_EPCell.h"
#import "SCBusinessInfo_EPView.h"
#import "SCGoodsCommentCell.h"
#import "SCAllEvaluaController.h"
#import "SCBuyImmediatelyController.h"

@interface SCEatPlayStoreDetailController ()<UITableViewDelegate,UITableViewDataSource,SCRecommendGoods_EPCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView      *headView;
@property (weak, nonatomic) IBOutlet UIImageView *logoImage;
@property (weak, nonatomic) IBOutlet UILabel     *nameLab;
@property (weak, nonatomic) IBOutlet UILabel     *priceLab;
@property (weak, nonatomic) IBOutlet UILabel     *timeLab;
@property (weak, nonatomic) IBOutlet UILabel     *addressLab;
@property (weak, nonatomic) IBOutlet UILabel     *stateLab;

@property (strong, nonatomic) SCModel *storeModel;
@property (strong, nonatomic) NSArray *foods_list;
@property (strong, nonatomic) NSArray *comment_list;

@end

@implementation SCEatPlayStoreDetailController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.lineView.hidden = NO;
    
    self.tableView.backgroundColor = BGColor;
    self.tableView.height = kScreenH-kTopHeight;
    //self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.separatorColor = RGB(235, 235, 235);
    [self.tableView registerNib:[UINib nibWithNibName:@"SCRecommendGoods_EPCell" bundle:nil] forCellReuseIdentifier:@"SCRecommendGoods_EPCellID"];
    [self.tableView registerClass:[SCGoodsCommentCell class] forCellReuseIdentifier:@"SCGoodsCommentCellID"];
    
    //请求数据
    [self requestData];
}
#pragma mark - 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.foods_list.count;
    }else{
        return self.comment_list.count;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 122;
    }
    return [self cellHeightForIndexPath:indexPath cellContentViewWidth:kScreenW tableView:self.tableView];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 45;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    SCDetailSetion_EPView *view = [SCDetailSetion_EPView addDetailSetion_EPView];
    view.frame = CGRectMake(0, 0, kScreenW, 35);
    view.moreBtn.tag = section;
    [view.moreBtn addTarget:self action:@selector(clickMoreBtn:) forControlEvents:UIControlEventTouchUpInside];
    if (section == 0) {
        view.titleLab.text = @"推荐商品";
        view.moreLab.hidden = YES;
    }else{
        view.titleLab.text = @"用户评价";
        view.moreLab.hidden = NO;
    }
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        SCRecommendGoods_EPCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCRecommendGoods_EPCellID"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.foods_list[indexPath.row];
        cell.delegate = self;
        return cell;
    }else{
        SCGoodsCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCGoodsCommentCellID"];
        cell.model = self.comment_list[indexPath.row];
        return cell;
    }
}
#pragma mark - 请求数据
- (void)requestData{
    
    NSString *url = [MyTool splicingWithUrl:@"admin.php?g=System&c=EdphApp&a=shop_details"];
    NSDictionary *dict = @{@"shop_id":self.shop_id,
                           @"size":@"2"};
    [SVProgressHUD showWithStatus:@"加载中..."];
    [[NetworkRequestTool sharedNetworkTool] POST:url parameters:dict finished:^(id result, NSString *error) {
        [SVProgressHUD dismiss];
        
        NSLog(@"===%@",result);
        
        self.storeModel = [SCModel mj_objectWithKeyValues:result[@"data"]];
        
        [self.logoImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HTTP_URL,self.storeModel.logo]] placeholderImage:[UIImage imageNamed:@""]];
        self.nameLab.text = self.storeModel.name;
        self.priceLab.text = [NSString stringWithFormat:@"¥%@",self.storeModel.permoney];
        self.timeLab.text = [NSString stringWithFormat:@"营业时间 | %@",self.storeModel.open_close];
        self.addressLab.text = self.storeModel.adress;
        if ([self.storeModel.have_waimai intValue] == 0) {
            self.headView.height = 181;
        }else{
            self.stateLab.text = [NSString stringWithFormat:@"（%@）",self.storeModel.is_open];
        }
        
        self.foods_list = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"foods_list"]];
        self.comment_list = [SCModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"comment_list"]];
        [self.tableView reloadData];
        
        //设置底部视图
        SCBusinessInfo_EPView *footView = [SCBusinessInfo_EPView addBusinessInfo_EPView];
        CGFloat H = 222+(kScreenW-24)*250/360+41+12;
        footView.frame = CGRectMake(0, 0, kScreenW, H);
        self.tableView.tableFooterView = footView;
        
        footView.bankingHoursLab.text = self.storeModel.open_close;
        [footView.zizhiImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.douyinhot.com/upload/authfile/%@",self.storeModel.auth_files]] placeholderImage:[UIImage imageNamed:@""]];
    }];
}
#pragma mark - 跳转外卖
- (IBAction)clickWaiMaiBtn:(UIButton *)sender {
    
    
}
#pragma mark - 更多
- (void)clickMoreBtn:(UIButton *)sender{
    if (sender.tag == 0) {//更多商品
        
    }else{//更多评价
        SCAllEvaluaController *vc = [[SCAllEvaluaController alloc]init];
        vc.type = @"shop_id";
        vc.goodsId = self.shop_id;
        vc.url = @"admin.php?g=System&c=EdphApp&a=evaluate";
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - 立即购买
- (void)clickBuyButtonWithModel:(SCModel *)model{
    SCBuyImmediatelyController *vc = [[SCBuyImmediatelyController alloc]init];
    vc.food_id = model.goods_id;
    vc.title = self.storeModel.name;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
